/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.units.examples;

import it.units.GoogleCommon.GeocodeException;
import it.units.GoogleElevation.ElevationResponse;
import it.units.GoogleCommon.Location;
import it.units.GoogleElevation.ElevationRequestor;
import it.units.GoogleElevation.Result;
import it.units.GoogleElevation.Status;
import it.units.GoogleGeocoding.GeocodeRequestor;
import it.units.GoogleGeocoding.GeocodeResponse;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

/**
 *
 * @author giorgio
 */
public class Main {

  //how to serialize with JAXB
  public static void writeResponse(String filename) {
    ElevationResponse response = new ElevationResponse();
    response.setStatus(Status.OVER_QUERY_LIMIT);

    Location location1 = new Location();
    location1.setLat(1f);
    location1.setLng(11f);
    Result result1 = new Result();
    result1.setLocation(location1);
    result1.setElevation(111f);
    response.getResults().add(result1);

    Location location2 = new Location();
    location2.setLat(2f);
    location2.setLng(22f);
    Result result2 = new Result();
    result2.setLocation(location2);
    result2.setElevation(222f);
    response.getResults().add(result2);

    try {
      JAXBContext context = JAXBContext.newInstance(response.getClass());
      Marshaller marshaller = context.createMarshaller();
      marshaller.marshal(response, new FileWriter(filename));
    } catch (JAXBException ex) {
      Logger.getLogger("Main").severe("error writing file: " + ex.toString());
    } catch (IOException ex) {
      Logger.getLogger("Main").severe("error writing file: " + ex.toString());
    }
  }


  public static void main(String[] args) {
    //Test elevation on multiple coordinates
    List<Location> locations = new ArrayList<Location>();
    Location location1 = new Location();
    location1.setLat(39.7391536f);
    location1.setLng(-104.9847034f);
    locations.add(location1);
    Location location2 = new Location();
    location2.setLat(36.455556f);
    location2.setLng(-116.866667f);
    locations.add(location2);

    ElevationRequestor requestor = new ElevationRequestor();
    ElevationResponse elevationResponse;
    try {
      elevationResponse = requestor.getElevations(locations);
      //single location
      //elevationResponse = requestor.getElevation(location1);
      System.out.println(elevationResponse);
    } catch (GeocodeException ex) {
      Logger.getLogger(Main.class.getName()).log(Level.SEVERE, "Error fetching Elevation", ex);
    }

   //Test geoCoding
    GeocodeRequestor geocodeRequestor = new GeocodeRequestor();
    //method1: formattedAddress
    String address = "1600+Amphitheatre+Parkway,+Mountain+View,+CA";
    //method2: reverse GeoCoding from coordinates
    Location trieste = new Location();
    trieste.setLat(45.64564f);
    trieste.setLng(13.76580f);
    //method3: list containing address lines
    List<String> addressLines = new ArrayList<String>();
    addressLines.add("Via Ciamician 11");
    addressLines.add("Trieste");
    addressLines.add("Italy");

    GeocodeResponse geocodeResponse;
    try{
      //geocodeResponse = geocodeRequestor.getGeocode(address);
      //geocodeResponse = geocodeRequestor.getGeocode(trieste);
      geocodeResponse = geocodeRequestor.getGeocode(addressLines);
      System.out.println(geocodeResponse);
    }catch (GeocodeException ex) {
      Logger.getLogger(Main.class.getName()).log(Level.SEVERE, "Error fetching Geocode", ex);
    }

  }
}
